package com.atldy.commerce.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * @version 1.0
 * @Author: lidongyang
 * @CreateTime: 2021-08-17 12:37
 * @Description： 商品分类
 */
@Getter
@AllArgsConstructor
public enum GoodsCategory {

    DIAN_QI("1001", "电器"),
    SHI_PIN("1002", "食品"),
    DIAN_NAO("1003", "电脑"),
    SHOU_JI("1004", "手机"),
    DIAN_SHI("1005", "电视");


    /**
     * 状态码
     */
    @EnumValue
    @JsonValue    //标记响应json值
    private final String code;

    /**
     * 状态描述
     */
    private final String description;


    /**
     * 根据code获取商品
     */
    public static GoodsCategory of(String code) {
        Objects.requireNonNull(code);
        return Stream.of(values())
                .filter(brandCategory -> brandCategory.getCode().equals(code))
                .findAny()
                .orElseThrow(() -> new IllegalArgumentException(code + "not exists"));
    }


    @Override
    public String toString() {
        return this.description;
    }
}